require(["common", "coAjax", "queryString", "routie", "hash_routie"], function(jSouper, coAjax, QueryString, routie, hash_routie) {

    var getClassifyTree = new Promise(function(resolve, reject) {
        coAjax.get(appConfig.bus_classify_tree, {
            bus_id: appConfig.bus_id
        }, function(result) {
            var list = result.result.childs || [];
            App.set('$Cache.classify.list', list);
            resolve(list)
        }, function(errorCode, err, errMsg) {
            alert('warn', errMsg);
            reject(errMsg);
        });
    });

    routie({
        '*': function(hash) {
            var qs = new QueryString(location.hash);
            var mainClassify = qs.get('main');
            var second = qs.get('second');
            var page = qs.get('page') || 0;
            var num = qs.get('num') || 12;
            // 获取分类树
            getClassifyTree.then(function(list) {
                if (list.length) {
                    // 获取当前分类信息
                    mainClassify ? true : mainClassify = list[0].classify._id;
                    // 获取二级分类
                    coAjax.get(appConfig.bus_classify_by_id + mainClassify, {
                        bus_id: appConfig.bus_id
                    }, function(result) {
                        var temp = result.result;
                        if (temp) {
                            App.set('$Cache.classify.mainClassify', temp.current_classify);
                            App.set('$Cache.classify.childlist', temp.child_classify_list);
                        }
                        if (second) {
                            coAjax.get(appConfig.bus_classify_by_id + second, {
                                bus_id: appConfig.bus_id
                            }, function(result) {
                                var temp = result.result;
                                if (temp) {
                                    App.set('$Cache.classify.second', temp.current_classify);
                                    App.set('$Cache.classify.current_classify', temp.current_classify);
                                }
                            })
                        } else {
                            App.set('$Cache.classify.current_classify', temp.current_classify);
                        }
                    });
                    // 获取商品列表
                    coAjax.get(appConfig.bus_classify_all_child_goods_by_id + (second || mainClassify), {
                        bus_id: appConfig.bus_id
                    }, function(result) {
                        var goodslist = result.result;
                        App.set('$Cache.classify.goodslist', goodslist);
                    });
                }
            });
        }
    })
})
